# 模块名和模块测试APP名称
MODULE_NAME        := 06_led_patform_id
MODULE_DEVICE_NAME := $(MODULE_NAME)_device
MODULE_DRIVER_NAME := $(MODULE_NAME)_driver
APP_NAME           := $(MODULE_NAME)_app
DTB_NAME           := imx6ull-alpha-emmc

ARCH               ?= arm
CURRENT_PATH       := $(shell pwd)
#=====================================================
INCDIRS 		   := ./ 			   
SRCDIRS			   := ./

INCLUDE			   := $(patsubst %, -I %, $(INCDIRS))
#=====================================================
# NFS 共享目录
TFTP_SERVER        ?= ~/3tftp
NFS_SERVER         ?= ~/4nfs

TFTP_DIR           ?= $(TFTP_SERVER)/imx6ull
ROOTFS_ROOT_DIR    ?= $(NFS_SERVER)/buildroot
#ROOTFS_MODULE_DIR ?= $(ROOTFS_ROOT_DIR)/lib/modules/4.1.15-gfea85803-dirty
ROOTFS_MODULE_DIR  ?= $(ROOTFS_ROOT_DIR)/app
#=====================================================
ifeq ($(KERNELRELEASE),)

# 选择可执行文件运行的平台
ifeq ($(ARCH), arm)
KERNELDIR         ?= ~/5ALPHA/linux-imx-rel_imx_4.1.15_2.1.0_ga
APP_GCC_TOOL      ?= arm-linux-gnueabihf-gcc
else
KERNELDIR         ?= /lib/modules/$(shell uname -r)/build
APP_GCC_TOOL      ?= gcc
endif

# 设备树文件源码目录及设备树名称
DT_IN_LINUX_DIR           := $(KERNELDIR)/arch/arm/boot/dts
DTS_IN_LINUX_FILE         := $(DT_IN_LINUX_DIR)/$(DTB_NAME).dts
DTB_IN_LINUX_FILE         := $(DT_IN_LINUX_DIR)/$(DTB_NAME).dtb
DTS_IN_MODULE_FILE        := $(CURRENT_PATH)/$(DTB_NAME).dts
DTB_IN_MODULE_FILE        := $(CURRENT_PATH)/$(DTB_NAME).dtb

# 编译模块和测试程序
modules:
	$(MAKE) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C $(KERNELDIR) M=$(CURRENT_PATH) modules $(INCLUDE)
	$(APP_GCC_TOOL) $(APP_NAME).c -Wall -o $(APP_NAME) $(INCLUDE)

modules_install:
	$(MAKE) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C $(KERNELDIR) M=$(CURRENT_PATH) modules INSTALL_MOD_PATH=$(ROOTFS) modules_install

dtb:
	@cp -v $(DTS_IN_MODULE_FILE) $(DT_IN_LINUX_DIR)
	$(MAKE) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C $(KERNELDIR) M=$(CURRENT_PATH) dtbs -j4
	@cp -v $(DTB_IN_LINUX_FILE) $(CURRENT_PATH)

# 拷贝相关文件到nfs共享目录
install:
	@sudo cp -v $(MODULE_DEVICE_NAME).ko $(ROOTFS_MODULE_DIR)
	@sudo cp -v $(MODULE_DRIVER_NAME).ko $(ROOTFS_MODULE_DIR)
	@sudo cp -v $(APP_NAME) $(ROOTFS_MODULE_DIR)
	@echo "insmod $(MODULE_DRIVER_NAME).ko" > load.sh
	@echo "insmod $(MODULE_DEVICE_NAME).ko" >> load.sh
	@echo "rmmod $(MODULE_DRIVER_NAME).ko" > unload.sh
	@echo "rmmod $(MODULE_DEVICE_NAME).ko" >> unload.sh
	@sudo cp -v load.sh $(ROOTFS_MODULE_DIR)
	@sudo cp -v unload.sh $(ROOTFS_MODULE_DIR)

	@sudo cp -v $(DTB_IN_MODULE_FILE) $(TFTP_DIR)

uninstall:
	@sudo rm -vf $(ROOTFS_MODULE_DIR)/$(MODULE_DEVICE_NAME).ko 
	@sudo rm -vf $(ROOTFS_MODULE_DIR)/$(MODULE_DRIVER_NAME).ko 
	@sudo rm -vf $(ROOTFS_MODULE_DIR)/load.sh
	@sudo rm -vf $(ROOTFS_MODULE_DIR)/unload.sh
	@sudo rm -vf $(ROOTFS_MODULE_DIR)/$(APP_NAME)
#   @sudo rm -vf $(TFTP_DIR)/$(DTB_NAME).dtb

.PHONY: clean
clean:
	rm -rf  *.o  *.ko  
	rm -rf .*.cmd  *.mod.*  modules.order  Module.symvers   .tmp_versions .cache.mk 
	rm -rf $(APP_NAME)
	rm -rf $(DTB_IN_MODULE_FILE)
	rm -rf load.sh unload.sh

help:
	@echo "\033[1;32m================================ Help ================================\033[0m"
	@echo "Ubuntu may need to add sudo:"
	@echo "insmod <module_name>.ko   # Load module"
	@echo "rmmod <module_name>       # Uninstall the module"
	@echo "dmesg -C                  # Clear the kernel print information"
	@echo "lsmod                     # Check the kernel modules that have been inserted"
	@echo "dmesg                     # View information printed by the kernel"
	@echo "file <module_name>.ko     # View \".Ko\" file information"
	@echo "make ARCH=arm             # arm platform"
	@echo "\033[1;32m======================================================================\033[0m"

print:
	@echo "KERNELDIR = $(KERNELDIR)"
	@echo "INCLUDE   = $(INCLUDE)"

	@echo "dtb information:"
	@echo "DTB_NAME           = $(DTB_NAME)"
	@echo "DT_IN_LINUX_DIR    = $(DT_IN_LINUX_DIR)"
	@echo "DTS_IN_LINUX_FILE  = $(DTS_IN_LINUX_FILE)"
	@echo "DTB_IN_LINUX_FILE  = $(DTB_IN_LINUX_FILE)"
	@echo "DTS_IN_MODULE_FILE = $(DTS_IN_MODULE_FILE)"
	@echo "DTB_IN_MODULE_FILE = $(DTB_IN_MODULE_FILE)"

else
CONFIG_MODULE_SIG = n
obj-m += $(MODULE_DEVICE_NAME).o
obj-m += $(MODULE_DRIVER_NAME).o
endif